package com.lam.mall.controller;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import com.lam.common.validate.ValidateUtil;
import com.lam.common.validate.model.ValidOperType;
import com.lam.mall.common.constant.MallConstant;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lam.framework.operLog.annotation.Log;
import com.lam.common.components.dict.annotation.DictAutoTransfer;
import com.lam.common.context.TokenUtils;
import com.lam.common.utils.StringUtils;
import com.lam.framework.annotation.ParamInject;
import com.lam.common.base.controller.BaseController;
import com.lam.common.base.domain.Result;
import com.lam.framework.operLog.enums.BusinessType;
import com.lam.mall.entity.TransFeeTemplate;
import com.lam.mall.param.TransFeeTemplateParam;
import com.lam.mall.service.TransFeeTemplateService;
import org.springframework.validation.annotation.Validated;
import com.lam.common.validate.group.Update;
import com.lam.common.validate.group.Add;

/**
 * 运费模板Controller
 * 
 * @author admin
 * @date 2023-04-21
 */
@Log(title = "运费模板")
@ParamInject
@RestController
@RequestMapping("/${adminPath}/mall/transFeeTemplate")
public class TransFeeTemplateController extends BaseController {

    @Autowired
    private TransFeeTemplateService transFeeTemplateService;

    /**
     * 查询运费模板列表
     */
    @Log(oper="分页查询")
    @DictAutoTransfer
    @PreAuthorize("@ss.hasPm('mall:transFeeTemplate:list')")
    @GetMapping("/list")
    public Result list(TransFeeTemplateParam param) {
    
    	param.setShopId(TokenUtils.getLoginUserShopId());
        startPage();
        List<TransFeeTemplate> list = transFeeTemplateService.selectList(param);
        return Result.ok(getTableData(list));
    }

    /**
     * 获取运费模板详细信息
     */
    @DictAutoTransfer
    @Log(oper="根据ID查询")
    @PreAuthorize("@ss.hasPm('mall:transFeeTemplate:query')")
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable("id") String id) {
    
        return Result.ok(transFeeTemplateService.selectById(id));
    }

    /**
     * 新增运费模板
     */
    @PreAuthorize("@ss.hasPm('mall:transFeeTemplate:add')")
    @Log(businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@Validated(Add.class) @RequestBody TransFeeTemplate transFeeTemplate) {
    
    	transFeeTemplate.setShopId(TokenUtils.getLoginUserShopId());
        ValidateUtil.validUnique(transFeeTemplate, ValidOperType.ADD, MallConstant.UNIQUE_TRANS_FEE_TEMPLATE_NAME, "运费模板名称已存在！");
        int n = transFeeTemplateService.insert(transFeeTemplate);
        if(n > 0) {
        	transFeeTemplateService.removeTransFeeTemplateItemsCache(transFeeTemplate.getId());
        }
        return toAjax(n);
    }

    /**
     * 修改运费模板
     */
    @PreAuthorize("@ss.hasPm('mall:transFeeTemplate:edit')")
    @Log(businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@Validated(Update.class) @RequestBody TransFeeTemplate transFeeTemplate) {
    
    	transFeeTemplate.setShopId(TokenUtils.getLoginUserShopId());
        ValidateUtil.validUnique(transFeeTemplate, ValidOperType.UPDATE, MallConstant.UNIQUE_TRANS_FEE_TEMPLATE_NAME, "运费模板名称已存在！");
        int n = transFeeTemplateService.update(transFeeTemplate);
        if(n > 0) {
        	transFeeTemplateService.removeTransFeeTemplateItemsCache(transFeeTemplate.getId());
        }
        return toAjax(n);
    }

    /**
     * 删除运费模板
     */
    @PreAuthorize("@ss.hasPm('mall:transFeeTemplate:remove')")
    @Log(businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public Result remove(@PathVariable String id) {
    
    	int n = transFeeTemplateService.deleteById(id);
        if(n > 0) {
        	for (String i : StringUtils.str2List(id)) {
        		transFeeTemplateService.removeTransFeeTemplateItemsCache(i);
        	}
        }
        return toAjax(n);
    }

}
